home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / ups / genpower.000 / genpower / genpower-1.0.1 / gentest.c < prev    next >
C/C++ Source or Header  |  1995-07-16  |  15KB  |  335 lines

  1. /************************************************************************/
  2. /* File Name            : gentest.c                                     */
  3. /* Program Name         : gentest                     Version: 1.0.1    */
  4. /* Author               : Tom Webster <webster@kaiwan.com>              */
  5. /* Created              : 1995/01/22                                    */
  6. /* Last Modified By     : Tom Webster                 Date: 1995/07/05  */
  7. /*                                                                      */
  8. /* Compiler (created)   : GCC 2.6.3                                     */
  9. /* Compiler (env)       : Linux 1.2.5                                   */
  10. /* ANSI C Compatable    : No                                            */
  11. /* POSIX Compatable     : Yes?                                          */
  12. /*                                                                      */
  13. /* Purpose              : Monitor the serial port connected to a UPS.   */
  14. /*                      : Allows DTR and/or RTS lines to be stet to     */                                                 
  15. /*                      : simulate genpowerd UPS monitoring software.   */
  16. /*                      : The gentest program will show the state of    */
  17. /*                      : the control lines in the serial connection.   */
  18. /*                      :                                               */
  19. /*                      : The primary purpose for gentest is to help    */
  20. /*                      : discover the settings for UPS monitoring      */
  21. /*                      : cables.                                       */
  22. /*                      :                                               */
  23. /*                      : The "-r" option will set the RTS control      */
  24. /*                      : line.  The "-d" optin will set the DTR        */
  25. /*                      : control line.                                 */
  26. /*                      :                                               */
  27. /*                      : WARNING: If your UPS supports an inverter     */
  28. /*                      : shutdown, either RTS or DTR will be used to   */
  29. /*                      : kill the inverter.  Tests should not be       */
  30. /*                      : performed if your system is using the UPS.    */
  31. /*                      : Specifically, the use of the "-r" and "-d"    */
  32. /*                      : options together is not recomended.           */
  33. /*                                                                      */
  34. /* Usage                : genpowerd [-r] [-d] <serial device>           */
  35. /*                      : i.e. genpowerd -r /dev/cua4                   */
  36. /*                      : or:  genpowerd -d /dev/cua4                   */
  37. /*                      : or:  genpowerd -r -d /dev/cua4                */
  38. /*                                                                      */
  39. /* Copyright            : GNU Copyleft                                  */
  40. /************************************************************************/
  41.  
  42. #include <sys/types.h>
  43. #include <sys/ioctl.h>
  44. #include <fcntl.h>
  45. #include <errno.h>
  46. #include <stdlib.h>
  47. #include <unistd.h>
  48. #include <stdio.h>
  49. #include <signal.h>
  50.  
  51. #define MAXSTRING 10
  52. #define MAXROW       6
  53. #define MAXCOL       2
  54.  
  55. /* Global Array */
  56. int statarray[MAXROW][MAXCOL];
  57.  
  58. int linestat(int, int);
  59. int tester(int, int);
  60. void stater(char *stat, int linestat, int oldstat);
  61. void stater2(char *stat, int linestat);
  62.  
  63. /* Main program. */
  64. int main(int argc, char **argv) {
  65.     int fd;
  66.     int rts_bit = TIOCM_RTS;
  67.     int dtr_bit = TIOCM_DTR;
  68.     int rts_set   = 0;
  69.     int dtr_set   = 0;
  70.         int status    = 1;
  71.     int current   = 0;
  72.     int next      = 1;
  73.     int x          = 0;
  74.     char *program_name;
  75.     char stat[MAXSTRING];
  76.     char tags[MAXSTRING][MAXROW] = { "DTR",
  77.                         "RTS",
  78.                        "CAR",
  79.                          "CTS",
  80.                        "DSR",
  81.                        "RNG" };
  82.     int force     = 1;
  83.  
  84.     /* Parse the command line */
  85.     program_name = argv[0];
  86.     while ((argc > 1) && (argv[1][0] == '-')) {
  87.         switch (argv[1][1]) {
  88.             case 'r':
  89.                 rts_set = 1;
  90.                 break;
  91.             case 'd':
  92.                 dtr_set = 1;
  93.                 break;
  94.             default:
  95.                 fprintf(stderr, "Usage: %s [-r] [-d] <device>\n", program_name);
  96.                 exit(1);
  97.         }                     /* switch (argv[1][1]) */
  98.         argv++;
  99.         argc--;
  100.     }                        /* while ((argc > 1) && (argv[1][0] == '-')) */
  101.     
  102.     /* Done with options, make sure that one port is specified */
  103.     if (argc != 2) {
  104.         fprintf(stderr, "Usage: %s [-r] [-d] <device>\n", program_name);
  105.                 exit(1);
  106.     }                        /* if (argc != 2) */
  107.     
  108.  
  109.     /*********************/
  110.     /* Monitor the line. */
  111.     /*********************/
  112.  
  113.       /* Open monitor device. */
  114.       if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  115.         fprintf(stderr, "%s: %s", argv[1], sys_errlist[errno]);
  116.         exit(1);
  117.       }                         /* if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) */
  118.  
  119.       /* Line is opened, so DTR and RTS are high. Clear them, can  */
  120.       /* cause problems with some UPSs if they remain set.         */
  121.       ioctl(fd, TIOCMBIC, &rts_bit);
  122.       ioctl(fd, TIOCMBIC, &dtr_bit);
  123.  
  124.       /* Set line(s) to provide power high to enable monitoring of line. */
  125.     if (rts_set) {
  126.           ioctl(fd, TIOCMBIS, &rts_bit);
  127.     }                        /* if (rts_set) */
  128.     if (dtr_set) {
  129.           ioctl(fd, TIOCMBIS, &dtr_bit);
  130.     }                        /* if (dtr_set) */
  131.  
  132.       /* Now sample the line. */
  133.       while(1) {
  134.  
  135.         status = linestat(fd, current);
  136.  
  137.         /* If force is set, force display of status */
  138.         if (force) {
  139.             status = force;
  140.             force = 0;
  141.         }                    /* if (force) */
  142.         
  143.         if (status)  {
  144.             printf("---------------\n");
  145.             stater2(stat, statarray[0][current]);
  146.             printf("%s = %s\n", tags[0], stat);
  147.             stater2(stat, statarray[1][current]);
  148.             printf("%s = %s\n\n", tags[1], stat);
  149.             for ( x=2; x < MAXROW; x++) {
  150.                 stater(stat, statarray[x][current], statarray[x][next]);
  151.                 printf("%s = %s\n", tags[x], stat);
  152.             }                 /* for ( x=2; x < MAXROW; x++) */
  153.         }                     /* if (status) */
  154.  
  155.         /* Change array depth and sleep 2 seconds. */
  156.  
  157.         if (current == 0) {
  158.             current = 1;
  159.             next = 0;
  160.         } else {
  161.             current = 0;
  162.             next = 1;
  163.         }                    /* if (current == 0) */
  164.         sleep(2);
  165.       }                         /* while(1) */
  166.       /* Never happens */
  167.       return(0);
  168. }                             /* main */
  169. /************************************************************************/
  170. /* End of Function main                                                 */
  171. /************************************************************************/
  172.  
  173. /************************************************************************/
  174. /* Function             : linestat                                      */
  175. /* Author               : Tom Webster <webster@kaiwan.com>              */
  176. /* Created              : 1995/02/05                                    */
  177. /* Last Modified By     :                             Date:             */
  178. /*                                                                      */
  179. /* Takes                : File handle of open serial port.              */
  180. /*                      : Layer of global to alter.                     */                                                
  181. /*                                                                      */
  182. /* Returns              : int containing result of test.                */
  183. /*                      :                                               */                                                
  184. /*                      : >= 1 == Line status has changed               */
  185. /*                      : 0 == Line status has not changed.             */
  186. /*                                                                      */
  187. /* Purpose              : Tests the serial lines to see if there have   */
  188. /*                      : been any changes, alters global array and     */
  189. /*                      : returns a positive int if changed.            */                                                
  190. /*                                                                      */
  191. /************************************************************************/
  192. int linestat(int fd, int layer)
  193. {
  194.     int flags;
  195.     int x = 0;
  196.     int otherlayer;
  197.     int status = 0;
  198.     int lines[MAXROW] = { TIOCM_DTR,
  199.             TIOCM_RTS,
  200.             TIOCM_CAR,
  201.             TIOCM_CTS,
  202.             TIOCM_DSR,
  203.             TIOCM_RNG };
  204.     
  205.     /* Compute the last layer from the current */
  206.     if (layer == 1) {
  207.         otherlayer = 0;
  208.     } else {
  209.         otherlayer = 1;
  210.     }
  211.     
  212.         /* Get the status. */
  213.         ioctl(fd, TIOCMGET, &flags);
  214.  
  215.     for(x=0; x < MAXROW; x++) {
  216.  
  217.         statarray[x][layer] = tester(lines[x], flags); 
  218.         if (statarray[x][layer] != statarray[x][otherlayer]) {
  219.             status++;
  220.         }
  221.     }    
  222.     return(status);
  223.  
  224. } /* EOF linestat */
  225. /************************************************************************/
  226. /* End of Function linestat                                             */
  227. /************************************************************************/
  228.  
  229.  
  230. /************************************************************************/
  231. /* Function             : tester                                        */
  232. /* Author               : Tom Webster <webster@kaiwan.com>              */
  233. /* Created              : 1995/01/22                                    */
  234. /* Last Modified By     :                             Date:             */
  235. /*                                                                      */
  236. /* Takes                : Line of serial connection to test (int).      */
  237. /*                      :                                               */
  238. /*                      : The current status of the serial connection   */
  239. /*                      : as an int.                                    */
  240. /*                                                                      */
  241. /* Returns              : int containing result of test.                */
  242. /*                      :                                               */
  243. /*                      : 1 == Tested line is in normal mode.           */
  244. /*                      : 0 == Tested line is in failure mode.          */
  245. /*                                                                      */
  246. /* Purpose              : Tests the condition of a serial control line  */
  247. /*                      : to see if it is in normal or failure mode.    */
  248. /*                                                                      */
  249. /************************************************************************/                                                
  250. int tester(int testline, int testflags) 
  251. {
  252.         int genreturn;
  253.         if ((testflags & testline)) {
  254.             /* ON Value Returned */
  255.                 genreturn = 1;
  256.         }else{
  257.             /* Off Value Returned */
  258.                 genreturn = 0;
  259.         }
  260.         return(genreturn);
  261. } /* EOF tester */
  262. /************************************************************************/
  263. /* End of Function tester                                               */
  264. /************************************************************************/
  265.  
  266. /************************************************************************/
  267. /* Function             : stater                                        */
  268. /* Author               : Tom Webster <webster@kaiwan.com>              */
  269. /* Created              : 1995/01/22                                    */
  270. /* Last Modified By     :                             Date:             */
  271. /*                                                                      */
  272. /* Takes                : Pointer to character array.                   */
  273. /*                      :                                               */
  274. /*                      : The current status of the serial connection   */
  275. /*                      : as an int.  (zero or non-zero values)         */
  276. /*                                                                      */
  277. /* Returns              : Places text string in provided array.         */
  278. /*                      : If line status is non-zero, "Low" is          */
  279. /*                      : returned.  Otherwise, "High" is returned.     */
  280. /*                                                                      */
  281. /* Purpose              : Converts the int status of serial control     */
  282. /*                      : line to human readable tags.                  */
  283. /*                                                                      */
  284. /************************************************************************/                                                
  285. void stater(char *stat, int linestat, int oldstat)
  286. {
  287.     if (linestat == oldstat) {
  288.         if (linestat) {
  289.             strcpy(stat, "High  ( )");
  290.         } else {
  291.             strcpy(stat, "Low   ( )");
  292.         }
  293.     } else {
  294.         if (linestat) {
  295.             strcpy(stat, "High  (*)");
  296.         } else {
  297.             strcpy(stat, "Low   (*)");
  298.         }
  299.     }
  300. }
  301. /************************************************************************/
  302. /* End of Function stater                                               */
  303. /************************************************************************/
  304.  
  305. /************************************************************************/
  306. /* Function             : stater2                                       */
  307. /* Author               : Tom Webster <webster@kaiwan.com>              */
  308. /* Created              : 1995/01/22                                    */
  309. /* Last Modified By     :                             Date:             */
  310. /*                                                                      */
  311. /* Takes                : Pointer to character array.                   */
  312. /*                      :                                               */
  313. /*                      : The current status of the serial connection   */
  314. /*                      : as an int.  (zero or non-zero values)         */
  315. /*                                                                      */
  316. /* Returns              : Places text string in provided array.         */
  317. /*                      : If line status is non-zero, "Set" is          */
  318. /*                      : returned.  Otherwise, "Cleared" is returned.  */
  319. /*                                                                      */
  320. /* Purpose              : Converts the int status of serial control     */
  321. /*                      : line to human readable tags.                  */
  322. /*                                                                      */
  323. /************************************************************************/                                                
  324. void stater2(char *stat, int linestat)
  325. {
  326.     if (linestat) {
  327.         strcpy(stat, "Set");
  328.     } else {
  329.         strcpy(stat, "Cleared");
  330.     }
  331. }
  332. /************************************************************************/
  333. /* End of Function stater2                                              */
  334. /************************************************************************/
  335.